package io;

import java.io.FileInputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.such.kit.datacast.CastCommons;
import com.such.kit.easyparam.ParamMap;
import com.such.kit.io.bean.XmlTemplateItemConfig;
import com.such.kit.io.helper.XmlTemplateHelper;
import com.such.kit.io.source.DatabaseIOSource;
import com.such.kit.io.source.MemoryIOSource;
import com.such.kit.io.source.MemoryMapIOSource;
import com.such.kit.io.template.XmlIOTemplate;
import com.such.kit.io.template.support.IOTemplate;
import com.such.kit.string.StringCommons;
import el.Function;

public class XmlIO {

	private DatabaseIOSource databaseIOSource;

	public XmlIO() throws Exception {
		/*
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		dataSource.setUrl("jdbc:oracle:thin:@10.0.1.168:1521:VMS");
		dataSource.setUsername("CRSDEVELOP");
		dataSource.setPassword("CRSDEVELOP");
		this.databaseIOSource = new DatabaseIOSource(dataSource);
		*/
	}

	public void ioTemplate() throws Exception {
		String basePath = getClass().getResource("").getPath();

		/*
		XmlIOTemplate xmlIO = new XmlIOTemplate(new FileInputStream(basePath + "xmlTemplateSimple.xml"));
		xmlIO.setFilterEmptyNode(true);
		xmlIO.output(this.databaseIOSource, basePath, "output");
		*/

		IOTemplate xmlIO = new XmlIOTemplate(new FileInputStream(basePath + "xml91001Template.xml"));
		MemoryIOSource memoryIOSource = new MemoryIOSource();
		xmlIO.input(memoryIOSource, basePath + "xml91001Resource.xml");
		for (String key : memoryIOSource.getInputKeys()) {
			System.out.println(key + " > " + memoryIOSource.getInputResult(key));
		}
		XmlTemplateItemConfig rootItemConfig = new XmlTemplateItemConfig("body", new ParamMap<String, String>().putParam("count_jbxx", "count"));
		rootItemConfig.addSubItemConfig(new XmlTemplateItemConfig("jbxx", "jbxx", "array"));
		Map<String, Object> data = XmlTemplateHelper.parseInputResult(memoryIOSource, rootItemConfig, null);
		System.out.println(data);
	}

	public static void memoryMapIOTemplate() throws Exception {
		String basePath = XmlIO.class.getResource("").getPath();
		XmlIOTemplate xmlIO = new XmlIOTemplate(new FileInputStream(basePath + "xxkjTemplate.xml"));
		xmlIO.setSegmentable(false);
		xmlIO.registerOutputFunction(Function.class);
		String json = "[{\"TB201_COL1\":\"一主一备：一个生产中心对应一个灾备中心\",\"XXKJ_DATA_TB202_24A\":[{\"TB202_COL6\":\"数据中心1\",\"TB202_COL7\":\",生产中心,同城灾备中心\",\"TB202_COL9\":\"1\",\"TB202_COL10\":\"2\",\"TB202_COL99\":\",IC卡\",\"TB202_COL103\":\"-empty-\",\"TB202_COL102\":\"123\",\"TB202_COL137\":\"121212\",\"XXKJ_DATA_TB20201_24A\":[{\"TB20201_COL138\":\"123\",\"TB20201_COL139\":\"同城\",\"TB20201_COL140\":\"1212121\",\"LOGIC_ID\":\"\",\"TB202_LOGIC_ID\":\"\"},{\"TB20201_COL138\":\"123\",\"TB20201_COL139\":\"同城1\",\"TB20201_COL140\":\"1212121\",\"LOGIC_ID\":\"\",\"TB202_LOGIC_ID\":\"\"}],\"LOGIC_ID\":\"\",\"TB2_LOGIC_ID\":\"\"},{\"TB202_COL6\":\"数据中心\",\"TB202_COL7\":\",生产中心,同城灾备中心\",\"TB202_COL9\":\"1\",\"TB202_COL10\":\"2\",\"TB202_COL99\":\",IC卡\",\"TB202_COL103\":\"-empty-\",\"TB202_COL102\":\"123\",\"TB202_COL137\":\"121212\",\"XXKJ_DATA_TB20201_24A\":[{\"TB20201_COL138\":\"123\",\"TB20201_COL139\":\"同城\",\"TB20201_COL140\":\"1212121\",\"LOGIC_ID\":\"\",\"TB202_LOGIC_ID\":\"\"},{\"TB20201_COL138\":\"123\",\"TB20201_COL139\":\"同城\",\"TB20201_COL140\":\"1212121\",\"LOGIC_ID\":\"\",\"TB202_LOGIC_ID\":\"\"}],\"LOGIC_ID\":\"\",\"TB2_LOGIC_ID\":\"\"}],\"LOGIC_ID\":\"\"}]";
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("ROOT", JSON.parseArray(json));
		MemoryMapIOSource memoryMapIOSource = new MemoryMapIOSource("ROOT");
		memoryMapIOSource.setOutputResult(result);
		String xxkjResource = xmlIO.output(memoryMapIOSource);
		System.out.println(xxkjResource);
		xmlIO.input(memoryMapIOSource, xxkjResource.getBytes(StringCommons.CHARSET_UTF8));
		System.out.println(memoryMapIOSource.getInputResult());
	}

	public static void main(String[] args) throws Exception {
		XmlIO.memoryMapIOTemplate();
	}

}
